/**
 *
 *    Class:     PanAjax Light
 *    Version:   1.0.0
 *    Coder:     Even Simon <even.simon@gmail.com>
 *    Powered by PolarBeatle.com   
 *
 **/


var PanAjax = {
	// Constants
	NAME: "PanAjax Lite",
	VERSION: "1.0.0",
	IS_IE: ((document.all) ? true : false),
	
	// XML variable
	XMLconnectionOpen: false,
	XMLcallbackFunction: null,
	XMLreq: null,
	
	// JSON variables
	JSONCount: 1,
	JSONdH: document.getElementsByTagName("head").item(0),

	// Other variables
	_GET: new Array(),
	
	// CONSTRUCT function
	__construct: function()
	{
		// Browser detection
		PanAjax.Browser = PanAjax.searchString(PanAjax.dataBrowser) || "An unknown browser";
		PanAjax.BrowserVersion = PanAjax.searchVersion(navigator.userAgent)
			|| PanAjax.searchVersion(navigator.appVersion)
			|| "An browser unknown version";
		PanAjax.OS = PanAjax.searchString(PanAjax.dataOS) || "An unknown OS";

		// QueryString
		PanAjax.readQueryString();

		// XML
		if( document.implementation.hasFeature("XPath", "3.0") )
		{
			XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
			{
				if( !xNode ) { xNode = this; }
				var oNSResolver = this.createNSResolver(this.documentElement)
				var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
				var aResult = [];
				for( var i = 0; i < aItems.snapshotLength; i++)
				{
					aResult[i] =  aItems.snapshotItem(i);
				}
		
				return aResult;
			}
			XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
			{
				if( !xNode ) { xNode = this; }
		
				var xItems = this.selectNodes(cXPathString, xNode);
				if( xItems.length > 0 )
				{
					return xItems[0];
				}
				else
				{
					return null;
				}
			}
			XMLDocument.prototype.value = function()
			{
				if(PanAjax.IS_IE)
				{
					return this.text;
				}
				else
				{
					return (this.selectSingleNode("text()") ? this.selectSingleNode("text()").nodeValue : "");
				}
			}
			Element.prototype.selectNodes = function(cXPathString)
			{
				if(this.ownerDocument.selectNodes)
				{
					return this.ownerDocument.selectNodes(cXPathString, this);
				}
				else{throw "For XML Elements Only";}
			}
			Element.prototype.selectSingleNode = function(cXPathString)
			{
				if(this.ownerDocument.selectSingleNode)
				{
					return this.ownerDocument.selectSingleNode(cXPathString, this);
				}
				else{throw "For XML Elements Only";}
			}
		}
	},

	// General functions
	isset: function(_Object){
		if("undefined" != typeof(_Object))
		{
			if(null != _Object)
			{
				if("" != _Object)
				{
					return true;
				}
			}
		}
		return false;
	},

	Error: function(_errorString, _errorComment)
	{
		if(PanAjax.isset(_errorString))
		{
			if(!PanAjax.isset(_errorComment))
			{
				_errorComment = false;
			}
			alert(PanAjax.NAME + " Error: " + _errorString + ((_errorComment) ? ("\n\n" + _errorComment) : ""));
			return;
		}
		alert(PanAjax.NAME + " Error: Unknown error");
		return;
	},

	boolVal: function(_value)
	{
		if("true" == _value)
		{
			return true;
		}
		else if("false" == _value)
		{
			return false;
		}
		else
		{
			return _value;
		}
	},

	// XML functions
	getNodeText: function(obj)
	{
		if(PanAjax.IS_IE)
		{
			return obj.text;
		}
		else
		{
			return (obj.selectSingleNode("text()") ? obj.selectSingleNode("text()").nodeValue : "");
		}
	},

	// Browser functions
	searchString: function (data)
	{
		for (var i=0;i<data.length;i++)
		{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			PanAjax.versionSearchString = data[i].versionSearch || data[i].identity;
			if(dataString)
			{
				if(dataString.indexOf(data[i].subString) != -1)
				{
					return data[i].identity;
				}
			}
			else if(dataProp)
			{
				return data[i].identity;
			}
		}
	},
	searchVersion: function (dataString)
	{
		var index = dataString.indexOf(PanAjax.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+PanAjax.versionSearchString.length+1));
	},

	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{
			string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],

	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	],

	// Read QueryString
	readQueryString: function()
	{
		qs = document.location.search.substring(1);
		qsAr = qs.split("&");
		for(var i=0;i<qsAr.length;i++)
		{
			qsVr = qsAr[i].split("=");
			PanAjax._GET[qsVr[0]] = qsVr[1];
		}
	},

	// Cookies functions
	getCookie: function(_cName)
	{
		var nq = _cName + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i<ca.length;i++)
		{
			var c = ca[i];
			while(c.charAt(0)==' ')
			{
				c = c.substring(1,c.length);
			}
			if(c.indexOf(nq) == 0)
			{
				return PanAjax.boolVal(c.substring(nq.length,c.length));
			}
		}
		return null;
	},

	setCookie: function(_name, _value, _days, _domain, _secure)
	{
		if(_days)
		{
			var date = new Date();
			date.setTime(date.getTime()+(_days*24*60*60*1000));
			var exp = "; expires="+date.toGMTString();
		}
		else
		{
			exp = "";
		}
		var dStr = "";
		var sStr = "";
		if(PanAjax.isset(_domain))
		{
			if(true == _domain)
			{
				dStr = "domain=" + PanAjax.getCookieDomain() + "; ";
			}
			else
			{
				dStr = "domain=" + _domain + "; ";
			}
		}
		if(PanAjax.isset(_secure))
		{
			if(true == _secure)
			{
				sStr = "secure";
			}
		}
		document.cookie = _name + "=" + _value + exp + "; " + dStr + "path=/; " + sStr;
	},

	getCookieDomain: function()
	{
		var hD = document.location.host;
		var dP = hostDomain.split('.');
		var dl = domainParts.length;
		return (dl <= 2) ? '.' + hD : '.' + (dP.slice(dl - 2)).join('.');
	},

	// Ajax-XML functions
	XMLisConnected: function()
	{
		return PanAjax.XMLconnectionOpen;
	},

	XMLgetConnection: function()
	{
		try
		{
			PanAjax.XMLreq = new XMLHttpRequest();
		}
		catch(tryms1)
		{
	    try
			{
				PanAjax.XMLreq = new ActiveXObject("Msxml2.XMLHTTP");
	    }
			catch (tryms2)
			{
				try
				{
					PanAjax.XMLreq = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (failed)
				{
					PanAjax.XMLreq = false;
				}
	    }
		}
		if(!PanAjax.XMLreq)
		{
			PanAjax.Error("General Error", "Could not get XMLHttpRequest.\n"+
			                               "Probably your browser is not yet compatible.");
			return false;
		}
		return true;
	},

	XMLHandler: function()
	{
		if(PanAjax.XMLreq.readyState == 4)
		{
			if(PanAjax.XMLreq.status == 200)
			{
				PanAjax.XMLcallbackFunction.onSuccess(PanAjax.XMLreq.responseXML);
			}
			else
			{
				PanAjax.Error("Connection Error", "Unknown status " + PanAjax.XMLreq.status);
			}
			PanAjax.XMLconnectionOpen = false;
		}
	},

	XMLRequest: function(_url, _method, _postData, _callback)
	{
		PanAjax.XMLreq = null;
		PanAjax.XMLcallbackFunction = null;
		if(!PanAjax.isset(_url))
		{
			PanAjax.Error("XML Request Error", "No URL set.");
		}
		if(!PanAjax.isset(_callback))
		{
			PanAjax.Error("XML Request Error", "No Callback function set.");
		}
		if(!PanAjax.isset(_method))
		{
			_method = "GET";
		}
		if(!PanAjax.isset(_postData))
		{
			_postData = null;
		}
		if(PanAjax.XMLisConnected())
		{
			PanAjax.Error("XML Request Error", "Connection is open.");
			return;
		}
		if(PanAjax.XMLgetConnection())
		{
			PanAjax.XMLconnectionOpen = true;
			PanAjax.XMLcallbackFunction = _callback;
			PanAjax.XMLreq.open(_method, _url, true);
			PanAjax.XMLreq.onreadystatechange = PanAjax.XMLHandler;
			PanAjax.XMLreq.send(_postData);
		}
	},
	
	// JSON functions
	JSONRequest: function(_url)
	{
		if(!PanAjax.isset(_url))
		{
			Error("JSON Request Error", "No URL provided.");
			return;
		}
		if(_url.indexOf("?") != -1)
		{
			_url += "&PanNcIe=" + (new Date()).getTime();
		}
		else
		{
			_url += "?PanNcIe=" + (new Date()).getTime();
		}
		var jsonId = "PanAjaxJSON_N" + PanAjax.JSONCount;
		var scrObj = document.createElement("script");
    scrObj.setAttribute("type", "text/javascript");
    scrObj.setAttribute("charset", "utf-8");
    scrObj.setAttribute("src", _url);
    scrObj.setAttribute("id", jsonId);
    PanAjax.JSONdH.appendChild(scrObj);
    PanAjax.JSONCount++;
    return jsonId;
	},
	
	JSONKillRequest: function(reqId)
	{
		if(!PanAjax.isset(reqId))
		{
			Error("JSON Error", "Request Id cannot be empty.");
			return;
		}
		PanAjax.JSONdH.removeChild(reqId);
	}
}

// General DOM functions
function $()
{
	var es = new Array();
	for(var i=0;i<arguments.length;i++)
	{
		var e = arguments[i];
		if("string" == typeof(e))
		{
			e = document.getElementById(e);
		}
		if(arguments.length == 1)
		{ 
			return e;
		}
		elements.push(e);
	}
	return es;
}

PanAjax.__construct();